

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import 'base_content.dart';
import 'base_model.dart';

abstract class BasePage<T extends BaseModel> {

  ///构建page内容
  Widget content(T model)=>Container();

  final BaseContent baseContent=BaseContent();

  T createModel();

  Widget onBuild(T model) {
    return Scaffold(
      resizeToAvoidBottomInset: resizeToAvoidBottomInset(),
      appBar: hasAppBra()
          ? AppBar(
          centerTitle: true,
          title: title(),
          leading: leading(),
          actions: action())
          : null,
      body: SafeArea(
        child: content(model),
      ),
    );
  }

  ///title左边的组件
  Widget leading(){
    return null;
  }

  ///title右边的组件
  List<Widget> action(){
    return [];
  }
  ///title组件
  Widget title(){
    return null;
  }
  ///初始化
  void init() {
    WidgetsFlutterBinding.ensureInitialized().addPostFrameCallback((timeStamp) {
      afterBuild(baseContent.context);
    });
  }
  ///在build之前
  void beforeBuild(BuildContext context) {
    this.baseContent.context=context;
  }

  ///在build之后。
  void afterBuild(BuildContext context) {}

  ///释放资源
  void dispose() {}

  ///键盘弹出是否把底部组件往上顶
  bool resizeToAvoidBottomInset()=>false;

  ///是否开启APP bar
  bool hasAppBra() => true;

  BaseModel modelOf<E extends BaseModel>({bool listen=true}) {
    return Provider.of<E>(baseContent.context,listen: listen);
  }
}
